package top.datacluster.basic.algorithm.leetcode;

public class LeetCode5 {

    public String longestPalindrome(String s) {

        String result = "";
        for (int i = 0; i < s.length(); i++){
            String temp1 = get(s, i, i + 1);
            String temp2 = get(s, i - 1, i + 1);
            if (temp1.length() > temp2.length()){
                if (temp1.length() > result.length()){
                    result = temp1;
                }
            }else{
                if (temp2.length() > result.length()){
                    result = temp2;
                }
            }

        }
        if ("".equals(result)){
            return String.valueOf(s.charAt(0));
        }
        return result;
    }

    private String get(String s, int start, int end){
        while (start >= 0 && end < s.length()){
            if (s.charAt(start) == s.charAt(end)){
                start--;
                end++;
            }else{
                break;
            }
        }
        return s.substring(start + 1, end);
    }

    public static void main(String[] args) {
        System.out.println(new LeetCode5().longestPalindrome("bb"));
    }
}
